home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / AOCE / Development Tools / Sample Code / Messaging Service Access Module / Internet PMSAM / Internet PMSAM source / parser.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-08  |  4.2 KB  |  236 lines  |  [TEXT/MPS ]

  1. /*-------------------------------------------------------------------
  2.  
  3. AOCE Post Office Protocol (POP) / Simple Mail Transfer Protocol (SMTP)
  4. Mail Service Access Module
  5.  
  6. written by Steve Falkenburg-- MacDTS
  7. ©1991-1993 Apple Computer, Inc.
  8.  
  9. --------------
  10. change history
  11. --------------
  12.  
  13. SJF        02/19/93    update for beta build    b1
  14. SJF        10/29/92    update to a11            a11
  15. SJF        06/08/92    update to a8            a8
  16. SJF        02/15/92    first working version    a4.5
  17. SJF        10/16/91    initial coding            a3
  18.  
  19. ---------------------------------------------------------------------*/
  20.  
  21. #ifndef __TYPES__
  22. #include <Types.h>
  23. #endif
  24.  
  25. #ifndef __MEMORY__
  26. #include <Memory.h>
  27. #endif
  28.  
  29. #ifndef __PACKAGES__
  30. #include <Packages.h>
  31. #endif
  32.  
  33. #include <ctype.h>
  34. #include <string.h>
  35.  
  36. #ifdef applec
  37. #include <strings.h>
  38. #endif
  39.  
  40. #include "const.h"
  41. #include "gwerrors.h"
  42. #include "utils.h"
  43.  
  44. #include "parser.h"
  45.  
  46. short CountLines(char *text)
  47. {
  48.     short numLines;
  49.     
  50.     for (numLines=0; *text!=0; text++)
  51.         if (*text==CR && *(text+1)==LF)
  52.             numLines++;
  53.     
  54.     return numLines;
  55. }
  56.  
  57.  
  58. short CountCRLines(char *text)
  59. {
  60.     short numLines;
  61.     
  62.     for (numLines=0; *text!=0; text++)
  63.         if (*text==CR)
  64.             numLines++;
  65.     
  66.     return numLines;
  67. }
  68.  
  69.  
  70. void GetWord(char **word,char **text)
  71. {
  72.     /* advance past white space */
  73.     
  74.     while (isspace(**text) || **text==',')
  75.         (*text)++;
  76.         
  77.     *word = *text;
  78.     
  79.     /* advance pointer past word */
  80.     
  81.     while (**text && !isspace(**text) && **text!=',')
  82.         (*text)++;
  83.  
  84.     /* advance past white space */
  85.  
  86.     while (isspace(**text) || **text==',')
  87.         (*text)++;
  88. }
  89.  
  90.  
  91. void GetField(char **word,char **text,char delimiter,short *dataLength)    // gets item from delimited list
  92. {
  93.     *dataLength = 0;
  94.     
  95.     /* advance past white space */
  96.     
  97.     while (**text==delimiter)
  98.         (*text)++;
  99.         
  100.     *word = *text;
  101.     
  102.     /* advance pointer past word */
  103.     
  104.     while (**text && **text!=delimiter) {
  105.         (*text)++;
  106.         (*dataLength)++;
  107.     }
  108.  
  109.     /* advance past white space */
  110.  
  111.     while (**text==delimiter)
  112.         (*text)++;
  113.         
  114.     while (isspace(**text) || **text==CR || **text==LF)
  115.         (*text)++;
  116. }
  117.  
  118.  
  119. void GetLine(char **line,char **text)
  120. {
  121.     *line = *text;
  122.     
  123.     while (**text && **text!=CR && **text!=LF)
  124.         (*text)++;
  125.  
  126.     while (**text==CR || **text==LF)
  127.         (*text)++;
  128. }
  129.  
  130.  
  131. void GetNumber(long *number,char **text)
  132. {
  133.     Str255 numStr;
  134.     char *theWord,*entryPtr;
  135.     
  136.     GetWord(&theWord,text);
  137.     
  138.     entryPtr = (char *)numStr;
  139.     while (*theWord && *theWord!=CR && *theWord!=LF && !isspace(*theWord))
  140.         *entryPtr++ = *theWord++;
  141.     *entryPtr++ = '\0';
  142.     c2pstr((char *)numStr);
  143.     
  144.     StringToNum(numStr,number);
  145. }
  146.  
  147.  
  148. void CopyWord(char *dest,char *sourceStream)
  149. {
  150.     while (*sourceStream && !isspace(*sourceStream) && *sourceStream!=',')
  151.         *dest++ = *sourceStream++;
  152.     *dest++ = '\0';
  153. }
  154.  
  155.  
  156. void CopyLine(char *dest,char *sourceStream)
  157. {
  158.     while (*sourceStream && *sourceStream!=CR && *sourceStream!=LF)
  159.         *dest++ = *sourceStream++;
  160.     *dest++ = '\0';
  161. }
  162.  
  163.  
  164. /* copies a line upto the next line starting with a non-white space in the first column */
  165.  
  166. void CopyAndUnfoldLine(char *dest,char *sourceStream)
  167. {
  168.     do {
  169.         while (*sourceStream && *sourceStream!=CR && *sourceStream!=LF)
  170.             *dest++ = *sourceStream++;
  171.         if (*sourceStream==CR) {
  172.             *sourceStream++;
  173.             if (*sourceStream==LF)
  174.                 *sourceStream++;
  175.         }
  176.     } while (*sourceStream && isspace(*sourceStream));
  177.     
  178.     *dest++ = '\0';
  179. }
  180.  
  181.  
  182. void StripLF(char *data,unsigned long *length)
  183. {
  184.     register char *source,*dest;
  185.     
  186.     if (*length > 0) {
  187.         source = dest = data;
  188.         while ((source - data) < (*length-1)) {
  189.             if (*source == LF && (source==data || *(source-1)==CR))
  190.                 source++;
  191.             else if (*source==LF) {
  192.                 *dest++ = CR;
  193.                 source++;
  194.             }
  195.             else
  196.                 *dest++ = *source++;
  197.         }
  198.         if (*source != LF && (source - data) < *length)
  199.             *dest++ = *source++;
  200.         *length = dest - data;
  201.         data[*length] = '\0';
  202.     }
  203. }
  204.  
  205.  
  206. OSErr AddLF(char *preLF,char **postLF)
  207. {
  208.     register char *old,*new;
  209.     short numLines;
  210.     unsigned long newLength,oldLength;
  211.     
  212.     oldLength = strlen(preLF);
  213.     numLines = CountCRLines(preLF);
  214.     newLength = oldLength + numLines + 256;    // leave some extra room for error and slop
  215.     
  216.     *postLF = NewPtrChk(newLength);
  217.     if (MemError()!=noErr)
  218.         return MemError();
  219.     
  220.     new = *postLF;
  221.     old = preLF;
  222.     
  223.     if (*old==LF)
  224.         old++;
  225.     
  226.     while ((old-preLF) < oldLength) {
  227.         *new++ = *old++;
  228.         if ( *(old-1)==CR && *old!=LF )
  229.             *new++ = LF;
  230.     }
  231.     
  232.     *new++ = '\0';    // null terminate new string
  233.     
  234.     return noErr;
  235. }
  236.